const config = require('../../common/config');
const util = require('../../utils/util');
var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
var qqmapsdk;

var userLongitude, userLatitude;

Page({
    data: {
        hasNewMsg: false,
        msgContent: '0条未读消息',
        slider: [],
        homeMenus: [],
        userInfo: {},
        hasUserInfo: false,
        currentTab: 0,
        loadStatus: false, //加载状态
        hasNewData: false,
        homeDataList: []
    },
    swichNav: function (e) {
        var that = this;
        if (this.data.currentTab === e.target.dataset.current) {
            return false;
        } else {
            that.setData({
                currentTab: e.target.dataset.current,
            })
        }

        getHomeDataList(that, this.data.currentTab);

    },
    onLoad: function () {
        var that = this;

        getHomeMenus(that);

        getSlider(that);

        // 实例化API核心类
        qqmapsdk = new QQMapWX({
            key: config.QQMAPSDK_KEY
        });

        //定位
        wx.getLocation({
            type: 'wgs84',
            success: function (res) {
                userLatitude = res.latitude
                userLongitude = res.longitude
                var speed = res.speed
                var accuracy = res.accuracy

                wx.setStorageSync(config.LOC_LATITUDE, userLatitude);
                wx.setStorageSync(config.LOC_LONGITUDE, userLongitude);

                getUptodateData(that);
            },
            fail: function (res) {
                util.debug("home/indexjs:获取经纬度失败:");
                util.debug(res);
                getUptodateData(that);
            }
        });

        setTimeout(function () {
            that.getMessage()
        }, 30000)
    },
    getMessage: function () {
        var that = this;
        wx.request({
            url: config.COMMPT_REQPATH,
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            method: 'POST',
            data: {
                accessToken: '',
                refreshToken: '',
                serviceName: 'messageService',
                methodName: 'getReceiveMsgCount',
                reqData: JSON.stringify({
                    receiver: [wx.getStorageSync(config.USER_ID), '101'],
                    messageStatus: ['0']
                })
            },
            success: function (res) {
                var resultData = res.data;
                wx.hideLoading();
                if (resultData.code == 1) {
                    var messageCount = resultData.data.messageCount;
                    if (messageCount > 0) {
                        that.setData({
                            hasNewMsg: true,
                            msgContent: messageCount + "条未读消息"
                        })
                    } else {
                        that.setData({
                            hasNewMsg: false,
                            msgContent: "0条未读消息"
                        })
                    }
                }
            }
        })
    }
});

//最新数据
function getUptodateData(thisPage) {
    util.debug("home/indexjs获取最新数据.");
    thisPage.setData({
        loadStatus: true,
        hasNewData: false
    })
    wx.request({
        url: config.COMMPT_REQPATH,
        header: {
            "Content-Type": "application/x-www-form-urlencoded"
        },
        method: 'POST',
        data: {
            accessToken: '',
            refreshToken: '',
            serviceName: 'wxShopManageService',
            methodName: 'getShopList',
            reqData: JSON.stringify({
                sortType: '1',
                latitute: userLatitude,
                longitude: userLongitude
            })
        },
        success: function (res) {
            var resultData = res.data;

            if (resultData.code == 1) {
                var bzShopList = resultData.data.bzShopList;
                if (bzShopList.length > 0) {
                    thisPage.setData({
                        homeDataList: bzShopList,
                        hasNewData: true,
                        loadStatus: false
                    })
                } else {
                    thisPage.setData({
                        homeDataList: [],
                        hasNewData: false,
                        loadStatus: false
                    })
                }
            }
        },
        fail: function (errResult) {
            thisPage.setData({
                loadStatus: false,
                homeDataList: [],
                hasNewData: false
            })
            util.debug(errResult);
        }
    })
}

//最近数据
function getShortRangeData(thisPage) {
    util.debug("home/indexjs获取最近数据.");
    thisPage.setData({
        loadStatus: true,
        hasNewData: false
    })
    wx.request({
        url: config.COMMPT_REQPATH,
        header: {
            "Content-Type": "application/x-www-form-urlencoded"
        },
        method: 'POST',
        data: {
            accessToken: '',
            refreshToken: '',
            serviceName: 'wxShopManageService',
            methodName: 'getShopList',
            reqData: JSON.stringify({
                sortType: '2',
                latitute: userLatitude,
                longitude: userLongitude
            })
        },
        success: function (res) {
            var resultData = res.data;

            if (resultData.code == 1) {
                var bzShopList = resultData.data.bzShopList;
                if (bzShopList.length > 0) {
                    thisPage.setData({
                        homeDataList: bzShopList,
                        hasNewData: true,
                        loadStatus: false
                    })
                } else {
                    thisPage.setData({
                        homeDataList: [],
                        hasNewData: false,
                        loadStatus: false
                    })
                }
            }
        },
        fail: function (errResult) {
            thisPage.setData({
                loadStatus: false,
                homeDataList: [],
                hasNewData: false
            })
            util.debug(errResult);
        }
    })
}

function getHomeMenus(pageOjb) {
    pageOjb.setData({
        homeMenus: [
            {
                imgPath: config.WEB_ROOT + "/images/icons/推荐_1.png",
                url: "../../pages/classlist/index?typeId=198&typeName=热门",
                menuName: "热门"
            },
            {
                imgPath: config.WEB_ROOT + "/images/icons/美食_1.png",
                url: "../../pages/classlist/index?typeId=101&typeName=美食",
                menuName: "美食"
            },
            {
                imgPath: config.WEB_ROOT + "/images/icons/电影_2.png",
                url: "../../pages/classlist/index?typeId=10408&typeName=电影",
                menuName: "电影"
            },
            {
                imgPath: config.WEB_ROOT + "/images/icons/酒店_1.png",
                url: "../../pages/classlist/index?typeId=103&typeName=酒店",
                menuName: "酒店"
            },
            {
                imgPath: config.WEB_ROOT + "/images/icons/全部_1.png",
                url: "../../pages/allstore/index",
                menuName: "更多"
            }
        ]
    })
}

function getSlider(pageOjb) {
    wx.showLoading({
        title: '加载中..',
        icon: 'loading',
        duration: config.TIME_OUT,
        mask: true
    })
    wx.request({
        url: config.COMMPT_REQPATH,
        header: {
            "Content-Type": "application/x-www-form-urlencoded"
        },
        method: 'POST',
        data: {
            accessToken: '',
            refreshToken: '',
            serviceName: 'wxHomePageService',
            methodName: 'selectHomeSwiper',
            reqData: ''
        },
        success: function (res) {
            var resultData = res.data;
            if (resultData.code == 1) {
                var swiperList = resultData.data.swiperList;
                var swipers = [];
                for (var i = 0; i < swiperList.length; i++) {
                    swipers.push({
                        imgPath: swiperList[i].filePath,
                        url: swiperList[i].linkUrl
                    })
                }
                pageOjb.setData({
                    slider: swipers
                })
            }
        },
        complete:function () {
            wx.hideLoading();
        }
    })
}

function getHomeDataList(thisPage, tabIndex) {
    thisPage.setData({
        homeDataList: []
    })
    if (tabIndex == 0) {
        getUptodateData(thisPage);
    } else if (tabIndex == 1) {
        getShortRangeData(thisPage);
    }
}